﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using KetBanBonPhuong;
using Telerik.Web.UI;

public partial class Admin_UserControls_GroupManagement : System.Web.UI.UserControl
{
    private static int GroupID = -1;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            Panel1.Visible = true;
            Panel2.Visible = false;
            Panel3.Visible = false;
        }
    }
    protected void RadGrid1_iTemcommand(object source, Telerik.Web.UI.GridCommandEventArgs e)
    {
        if (e.CommandName == "Xem")
        {
            GridDataItem item = (GridDataItem)e.Item;
            GroupID = int.Parse(item["group_id"].Text);
            LoadData();
            Panel1.Visible = false;
            Panel2.Visible = true;
            return;
        }

        if (e.CommandName == "Sua")
        {
            GridDataItem item = (GridDataItem)e.Item;
            GroupID = int.Parse(item["group_id"].Text);
            LoadData();
            Panel1.Visible = false;
            Panel2.Visible = true;
            return;
        }

        if (e.CommandName == "Xoa")
        {
            GridDataItem item = (GridDataItem)e.Item;
            GroupID = int.Parse(item["group_id"].Text);
            Group.Delete(GroupID);
            return;
        }

        if (e.CommandName == "MemberManager")
        {
            GridDataItem item = (GridDataItem)e.Item;
            GroupID = int.Parse(item["group_id"].Text);
            Panel1.Visible = false;
            Panel3.Visible = true;
            return;
        }

    }


    protected void RadGrid1_PreRender(object sender, EventArgs e)
    {
        DataTable dt = Group.GetAll();
        dt.Columns.Add("username");
        foreach (DataRow dtRow in dt.Rows)
        {
            User user = User.GetInfo((int)dtRow["user_id"]);
            dtRow["username"] = user.UserName;
        }
        RadGrid1.DataSource = dt;
        RadGrid1.DataBind();
    }
    protected void Button_OK_Click(object sender, EventArgs e)
    {
        Group group = Group.GetInfo(GroupID);
        group.Group_name = TextBox_GroupName.Text.Trim();
        group.About_group = TextBox_About.Text.Trim();
        group.Interests = TextBox_Interest.Text.Trim();
        group.Phone = TextBox_Phone.Text.Trim();
        if (RadioButtonList1.SelectedIndex == 0)
        {
            group.Is_activated = true;
        }
        else
        {
            group.Is_activated = false;
        }
        try
        {
            Group.Update(group);
            Panel1.Visible = true;
            Panel2.Visible = false;
        }
        catch (System.Exception Ex)
        {
            Label_Result.Text = Ex.ToString();
        }
    }

    protected void LoadData()
    {
        Group group = Group.GetInfo(GroupID);
        Label_UserName.Text = User.GetInfo(group.User_id).UserName;
        TextBox_GroupName.Text = group.Group_name;
        TextBox_About.Text = group.About_group;
        TextBox_Interest.Text = group.Interests;
        TextBox_Phone.Text = group.Phone;
        if (group.Is_activated)
        {
            RadioButtonList1.SelectedIndex = 0;
        }
        else
        {
            RadioButtonList1.SelectedIndex = 1;
        }
        Label_CreateDate.Text = String.Format("{0,2:00}:{1,2:00}:{2,2:00} - {3,2:00}/{4,2:00}/{5,4}",
                group.Created_date.Hour, group.Created_date.Minute, group.Created_date.Second,
                group.Created_date.Day, group.Created_date.Month, group.Created_date.Year);
        return;
    }
    protected void RadGrid2_PreRender(object sender, EventArgs e)
    {
        int MemberListID = MemberList.GetMemberListID(GroupID);
        DataTable dt = Member.GetAll(MemberListID);
        dt.Columns.Add("username", typeof(String));
        dt.Columns.Add("subscriber", typeof(String));
        foreach (DataRow dtRow in dt.Rows)
        {
            dtRow["username"] = User.GetInfo((int)dtRow["member_user_id"]).UserName;
            if ((bool)dtRow["is_subscriber"])
            {
                dtRow["subscriber"] = "Đã được chấp nhận";
            }
            else
            {
                dtRow["subscriber"] = "Chưa được chấp nhận";
            }
        }
        RadGrid2.DataSource = dt;
        RadGrid2.DataBind();
    }
    protected void RadGrid2_iTemcommand(object source, GridCommandEventArgs e)
    {
        if(e.CommandName == "Xoa")
        {
            GridDataItem item = (GridDataItem)e.Item;
            Member.Delete(int.Parse(item["member_id"].Text));
        }
    }
    protected void Button_Back_Click(object sender, EventArgs e)
    {
        Panel1.Visible = true;
        Panel2.Visible = false;
    }
    protected void Button_Quaylai_Click(object sender, EventArgs e)
    {
        Panel2.Visible = false;
        Panel1.Visible = true;
    }
}
